<?php
include_once("tpl.php");
$main = checkHtml();
echo tpl($main);
?>
<?php
function checkHtml()	{
	$sHtml = "";
	if(isset($_GET["mod"]))	{
		$mod	=	$_GET["mod"];
	}
	else	{
		$mod	=	"twoball";
	}

	include_once("inc/lotteryMod.php");
	$aMod	=	lotteryMod("lotteryMod/".$mod.".csv");
	include_once("inc/sNum2aNum.php");

	if(isset($_GET["num"]))	{
		$strNum	=	$_GET["num"];
		$arrSim	=	sNum2aNum($strNum,$aMod);
	}
	else	{
		$strNum	=	"4,11,14,17,18,27,16";
		$arrSim	=	sNum2aNum($strNum,$aMod);
	}

	$sHtml.= '<a href="check.php?mod=twoball&num=2,3,5,8,13,21,1">双色球</a> ';
	$sHtml.= '<a href="check.php?mod=lotto&num=3,5,8,13,21,1,2">大乐透</a> ';
	$sHtml.= '<a href="check.php?mod=26s5&num=3,5,8,13,21">福彩26选5</a> ';
	$sHtml.= '<a href="check.php?mod=3d&num=2,3,5">福彩3D</a>';
	$sHtml.= "<hr />";
	include_once("inc/file2history.php");
	$aHistoryM	=	file2history("history/".$mod.".csv");
	//$aHistory	=	array_pop($aHistory);
	$highest_bingo["rank"]	=	9;
	$highest_bingo["history"]	=	9;
	foreach($aHistoryM as $aHistory)	{
		include_once("inc/lotteryMod.php");
		$aMod	=	lotteryMod("lotteryMod/".$mod.".csv");
		$aSimBingo	=	csvSimCheck($arrSim,$aHistory,$aMod);
		include_once("inc/getBingoLvl.php");
		$aBingoLvl	=	getBingoLvl("bingo/".$mod.".csv");

		$sHistory = implode(",",$aHistory);
		$aHistory = sNum2aNum($sHistory,$aMod);
		$sHtml.=arr2html($aHistory,$aMod);
		$sHtml.= "<br />";
		
		$sHtml.=arr2html($arrSim,$aMod);
		$sHtml.= "<br />";
		$bC	=	bingoCheck($aSimBingo,$aBingoLvl);
		if($bC)	{
			if((int)$bC<(int)$highest_bingo["rank"])	{
				$highest_bingo["rank"]=$bC;
				$highest_bingo["history"]=arr2html($aHistory,$aMod);
			}
		}
		$sHtml.= bingoCheck($aSimBingo,$aBingoLvl);
		$sHtml.= "<br />";
	}
	$sHtml.= "<hr />";
	$sHtml.= $highest_bingo["rank"];
	$sHtml.= "<br />";
	$sHtml.= $highest_bingo["history"];
	return $sHtml;
}
?>
<?php
function arr2html($arr,$aMod)	{
	$sHtml = "";
	for($i=0;$i<$aMod["sector"];$i++)	{
		$sHtml.=$i.":";
		$sHtml.=	implode(",",$arr[$i]);
		$sHtml.="<br />";
	}

	return $sHtml;
}
?>
<?php
function csvSimCheck($arrSim,$aHistory,$aMod)	{
	$strHistory	=	implode(",",$aHistory);
	$aHistory	=	sNum2aNum($strHistory,$aMod);
	$aBingo	=	singleCheck($arrSim,$aHistory);
	return $aBingo;
}
?>
<?php
function singleCheck($aSim,$aHistory)	{
	$sector = count($aSim);
	$bingo=array();
	for($i=0;$i<$sector;$i++)	{
		$bingo[$i]=0;
	}
	foreach($aSim as $key => $sim)	{
		foreach($sim as $index=>$num)	{
			if(in_array((int)$num,$aHistory[$key]))	{
				$bingo[$key]++;
			}
		}
	}
	return $bingo;
}
?>
<?php
function bingoCheck($aSimBingo,$aBingoLvl)	{
	$level = False;
	foreach($aBingoLvl as $lvl => $singleLvl)	{
		if(in_array($aSimBingo,$singleLvl))	{
			if($level <$lvl)	{
				$level = $lvl;
			}
		}
	}
	return $level;
}
?>